package com.example.chapter08.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;

import com.example.chapter08.R;
import com.example.chapter08.util.ToastUtil;

public class LaunchFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public LaunchFragment() {
        // Required empty public constructor
    }

    public static LaunchFragment newInstance(int position, int image_id, int count) {
        LaunchFragment fragment = new LaunchFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putInt("image_id", image_id);
        args.putInt("count", count);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        assert arguments != null;
        int position = arguments.getInt("position", 0);
        int image_id = arguments.getInt("image_id", 0);
        int count = arguments.getInt("count", 0);


        Context context = getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_launch, container, false);
        ImageView iv_launch = view.findViewById(R.id.fragment_iv_launch);
        RadioGroup rg_indicate = view.findViewById(R.id.fragment_rg_indicate);
        Button btn_start = view.findViewById(R.id.fragment_btn_start);

        iv_launch.setImageResource(image_id);

        // 每个页面都分配一组对应的单选按钮
        for (int j = 0; j < count; j++) {
            RadioButton radioButton = new RadioButton(context);
            radioButton.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            ));
            radioButton.setPadding(10, 10, 10, 10);
            rg_indicate.addView(radioButton);
        }

        // 当前位置的单选按钮要高亮现实，比如第二个引导页就高亮第二个单选按钮
        ((RadioButton) rg_indicate.getChildAt(position)).setChecked(true);

        // 最后一页显示“立即体验”按钮
        if (position == count - 1) {
            btn_start.setVisibility(View.VISIBLE);
            btn_start.setOnClickListener(v -> {
                ToastUtil.show(context, "欢迎进入主界面");
            });
        } else {
            btn_start.setVisibility(View.GONE);
        }


        return view;
    }
}